<?php
namespace app\user\controller;

use think\Config;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Base extends Controller
{
    protected $user_id;
    protected function _initialize()
    {
        //继承父类的初始化操作
        parent::_initialize();

        //系统参数
        $Config = Config();
        $this->assign('title',$Config['web_name']);
        $this->assign('tips',$Config['tips']);

//        if ($Config['web_switch'] == 0){
//            $this->redirect('Open/WebOff');
//        }
        //获取登录Session
        $UserID = Session::get('user_id');

        if(empty($UserID) || $UserID==null){
            $this->redirect('Open/password');
        }else{
            //根据Session值获取用户信息
            $UserInfo = UserInfo(['id'=>$UserID]);
            //如果用户信息不存在删除Session重新登陆
            if ($UserInfo['status'] != 1){
                Session::delete('user_id');
                $this->redirect('Open/password');
            }
            if(empty($UserInfo)){
                Session::delete('user_id');
                $this->redirect('Open/password');
            }
            $this->user_id = $UserInfo['id'];
//            $tj_url = "http://".$_SERVER['HTTP_HOST'].'/Open/Register/recommend/'.$UserInfo['recommend_num'];
//            $this->assign('UserInfo',$UserInfo);
//            $this->assign('tj_url',$tj_url);
        }
//        $get = Param();
//        if (isset($get['goods'])){
//            if ($get['goods'] == 500){
//                cookie('cart_type',1);
//            }elseif ($get['goods'] == 1000){
//                cookie('cart_type',2);
//            }
//        }
        //获取当前控制器+方法
        $con_act = Request::instance()->controller().'/'.Request::instance()->action();
        $this->assign('con_act',strtolower($con_act));

        //语言
        $lang_type = Config::get('lang_type');
        $lang_arr = Config::get('lang_list');
        $index = array_search(cookie('think_var'),$lang_arr);
        unset($lang_arr[$index]);
        $this->assign('lang_type',$lang_type);
        $this->assign('lang_arr',$lang_arr);
        //第二层的网体  本位球
//        $map_layer2_count = Db::name('user_map')->where(['user_id'=>$this->user_id,'map_level'=>2,'position'=>['neq',4],'status'=>1])->count();
//        $this->assign('map_layer2_count',$map_layer2_count);
    }

    public function _empty(){

    }

    public function success($msg = '', $data = null)
    {
        $result = [
            'code' => 0,
            'msg'  => $msg,
            'time' => time(),
            'data' => $data ? $data : new \stdClass(),
        ];

        return json_encode($result,JSON_UNESCAPED_UNICODE);
    }
}